package cc.admin.modules.wea.service.impl;

import cc.admin.common.util.RedisUtil;
import cc.admin.modules.wea.entity.*;
import cc.admin.modules.wea.mapper.WeaPlatformMapper;
import cc.admin.modules.wea.service.IWeaPlatformService;
import cc.admin.modules.wea.utils.WeatherUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.stringtemplate.v4.ST;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description: 平台基本信息表
 * @Author: cc-admin
 * @Date:   2024-09-03
 * @Version: V1.0.0
 */
@Service
public class WeaPlatformServiceImpl extends ServiceImpl<WeaPlatformMapper, WeaPlatform> implements IWeaPlatformService {

    @Autowired
    private IWeaPlatformService weaPlatformService;
    private static Map<String, Object> warnType = new HashMap<>();
    private static List<String> typeList = new ArrayList<>();
    static {
        typeList.add("Wave0");
        typeList.add("Wave1");
        typeList.add("Storm0");
        typeList.add("Storm1");
        typeList.add("SeaIce");
        typeList.add("Tsunami");

        warnType.put("Wave0","海浪警报");
        warnType.put("Wave1","海浪警报");
        warnType.put("Storm0","风暴潮警报");
        warnType.put("Storm1","风暴潮警报");
        warnType.put("SeaIce","海冰警报");
        warnType.put("Tsunami","海啸警报");
    }
    @Autowired
    WeaPlatformMapper weaPlatformMapper;
    @Autowired
    RedisUtil redisUtil;

    @Override
    public List<WeaPlatform> getPlatformAll() {
        return weaPlatformMapper.getPlatformAll();
    }



    @Override
    public WeaSevenForecast getSevenForecast(String platformId) {
        WeaSevenForecast WSF = new WeaSevenForecast(JSONObject.parseObject((String) redisUtil.get("SevenForecast"+platformId)));
        WSF.setPlatformId(weaPlatformMapper.selectById(platformId).getName());
        return WSF;
    }
/*获取七天天气预警*/
    @Override
    public List<Object> getSevenDayWeather(String platformId) {
        QueryWrapper<WeaPlatform> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", platformId);
        List<WeaPlatform> list = weaPlatformService.list(queryWrapper);
        for(WeaPlatform platform:list){
            String location= platform.getLon()+","+platform.getLat();
            String weatherData = WeatherUtil.getWeather(WeatherConstant.SEVEN_DAY_FORECAST, location);
            JSONArray array = JSONUtil.parseArray(weatherData);
            List<Object> weaList = CollUtil.newArrayList(array);
            return weaList;
        }


        return null;
    }

    @Override
    public List<Object> getSevenDayWeatherWarn(String id) {
        if(id.contains(",")){
            List<String> ids = Arrays.stream(id.split(","))
                    .collect(Collectors.toList());
            List<WeaWarningRecord> warnList=weaPlatformMapper.getWarnIds(ids);
            List<Object> array=new ArrayList<>();
            warnList.forEach(data->{
                String warnContent=(String) redisUtil.get(data.getWarningType()+"Record"+data.getPlatformId());
                if (StrUtil.isNotEmpty(warnContent)){
                    cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(warnContent);
                    array.add(jsonObject);
                }

            });

            return array;
        }
        List<WeaWarningRecord> warnList=weaPlatformMapper.getWarnId(id);
        List<Object> array=new ArrayList<>();
        warnList.forEach(data->{
            String warnContent=(String) redisUtil.get(data.getWarningType()+"Record"+data.getPlatformId());
            if (StrUtil.isNotEmpty(warnContent)){
                cn.hutool.json.JSONObject jsonObject = JSONUtil.parseObj(warnContent);
                array.add(jsonObject);
            }
        });
        return array;
    }

    @Override
    public List<WarnInfo> getWarnInfoList(String platformId) {
        List<WarnInfo> warnings = new ArrayList<>();
        if("".equals(platformId) || platformId == null){
            List<WeaPlatform> platformAll = weaPlatformMapper.getPlatformAll();
            for (WeaPlatform platform : platformAll) {
                warnings.addAll(getWarnInfoList(platform));
            }
        }else {
            WeaPlatform platform = weaPlatformMapper.selectById(platformId);
            warnings.addAll(getWarnInfoList(platform));
        }
        return warnings;
    }

    public List<WarnInfo> getWarnInfoList(WeaPlatform platform) {
        List<WarnInfo> warnings = new ArrayList<>();
        for (String type : typeList){
            if ("Tsunami".equals(type)) {
                for (int i = 0; i < 5;i++){
                    WarnInfo warnInfo = getWarnInfo(platform, type + i);
                    if (warnInfo != null) {
                        warnings.add(warnInfo);
                    }
                }
            }else {
                WarnInfo warnInfo = getWarnInfo(platform, type);
                if (warnInfo != null) {
                    warnings.add(warnInfo);
                }
            }
        }
        return warnings;
    }

    public WarnInfo getWarnInfo(WeaPlatform platform,String type){
        WarnInfo WI = null;
        String data  = (String) redisUtil.get(type);
        if(data != null && !"".equals(data)){
            WI = new WarnInfo();
            JSONObject jsonObject = JSONObject.parseObject(data);
            WI.setId(jsonObject.getString("id"));
            WI.setTitle(jsonObject.getString("title"));
            WI.setReleaseTime(jsonObject.getDate("releaseDate"));
            WI.setType(type);
            WI.setPlatformName(platform.getName());
            switch(jsonObject.getString("warningLevel")) {
                case "0":
                    WI.setColor("无警报");
                    break;
                case "1":
                    WI.setColor("蓝色警报");
                    break;
                case "2":
                    WI.setColor("黄色警报");
                    break;
                case "3":
                    WI.setColor("橙色警报");
                    break;
                case "4":
                    WI.setColor("红色警报");
                    break;
                default:
                    WI.setColor("");
                    break;
            }
        }
        return WI;
    }
}
